home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Kant Generator Pro 1.0.1 / source / Shell ƒ / text twiddling.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  9.3 KB  |  466 lines  |  [TEXT/MMCC]

  1. #include "text twiddling.h"
  2. #include "window layer.h"
  3.  
  4. #define RequireWindow(x)    if (theWindow==0L) return x
  5. #define RequireTE(x)        {    \
  6.                                 hTE=GetWindowTE(theWindow);    \
  7.                                 if (hTE==0L)    return x;    }
  8.  
  9. static    asm void MyDrawHook(void);
  10.  
  11. static    ProcPtr        gOldDrawHook;
  12.  
  13. Boolean AnyTextInScrapQQ(void)
  14. {
  15.     long            dummy;
  16.     
  17.     LoadScrap();
  18.     return (GetScrap(0L, 'TEXT', &dummy)!=noTypeErr);
  19. }
  20.  
  21. void SetTheText(WindowPtr theWindow, Ptr data, long count)
  22. {
  23.     TEHandle        hTE;
  24.     ControlHandle    vScrollBar;
  25.     
  26.     RequireWindow();
  27.     RequireTE();
  28.     vScrollBar=GetWindowVScrollBar(theWindow);
  29.     
  30.     TESetText(data, count, hTE);
  31.     TESetSelect(0, 0, hTE);
  32.     if (vScrollBar!=0L)
  33.         AdjustVScrollBar(vScrollBar, hTE);
  34. }
  35.  
  36. Boolean AnyTextQQ(WindowPtr theWindow)
  37. {
  38.     TEHandle        hTE;
  39.     
  40.     RequireWindow(FALSE);
  41.     RequireTE(FALSE);
  42.     
  43.     return ((**hTE).teLength!=0);
  44. }
  45.  
  46. Boolean AnyHighlightedQQ(WindowPtr theWindow)
  47. {
  48.     TEHandle        hTE;
  49.     
  50.     RequireWindow(FALSE);
  51.     RequireTE(FALSE);
  52.     
  53.     return ((**hTE).selStart!=(**hTE).selEnd);
  54. }
  55.  
  56. short SelectionStart(WindowPtr theWindow)
  57. {
  58.     TEHandle        hTE;
  59.     
  60.     RequireWindow(-1);
  61.     RequireTE(-1);
  62.     
  63.     return (**hTE).selStart;
  64. }
  65.  
  66. short SelectionEnd(WindowPtr theWindow)
  67. {
  68.     TEHandle        hTE;
  69.     
  70.     RequireWindow(-1);
  71.     RequireTE(-1);
  72.     
  73.     return (**hTE).selEnd;
  74. }
  75.  
  76. Boolean InsertBeforeStart(WindowPtr theWindow, Str255 theStr)
  77. {
  78.     TEHandle        hTE;
  79.     unsigned long    len;
  80.     unsigned long    oldLen;
  81.     
  82.     RequireWindow(FALSE);
  83.     RequireTE(FALSE);
  84.     
  85.     len=theStr[0];
  86.     hTE=GetWindowTE(theWindow);
  87.     oldLen=(**hTE).teLength;
  88.     if (oldLen+len>32767)
  89.         return FALSE;
  90.     
  91.     TEInsert(&theStr[1], len, hTE);
  92.     SetWindowIsModified(theWindow, TRUE);
  93.     return TRUE;
  94. }
  95.  
  96. Boolean InsertAfterEnd(WindowPtr theWindow, Str255 theStr)
  97. {
  98.     TEHandle        hTE;
  99.     unsigned long    len;
  100.     unsigned long    oldLen;
  101.     unsigned long    oldSelStart;
  102.     unsigned long    oldSelEnd;
  103.     
  104.     RequireWindow(FALSE);
  105.     RequireTE(FALSE);
  106.     
  107.     len=theStr[0];
  108.     hTE=GetWindowTE(theWindow);
  109.     oldLen=(**hTE).teLength;
  110.     if (oldLen+len>32767)
  111.         return FALSE;
  112.     
  113.     oldSelStart=(**hTE).selStart;
  114.     oldSelEnd=(**hTE).selEnd;
  115.     TESetSelect(oldSelEnd, oldSelEnd, hTE);
  116.     TEInsert(&theStr[1], len, hTE);
  117.     TESetSelect(oldSelStart, oldSelEnd, hTE);
  118.     SetWindowIsModified(theWindow, TRUE);
  119.     return TRUE;
  120. }
  121.  
  122. short TotalNumberOfLines(TEHandle hTE)
  123. {
  124.     short            numLines;
  125.     
  126.     if (hTE==0L)
  127.         return -1;
  128.     
  129.     numLines=(**hTE).nLines;
  130.     if (*((unsigned char*)((long)(*((**hTE).hText))+(**hTE).teLength-1))==0x0d)
  131.         numLines++;
  132.     
  133.     return numLines;
  134. }
  135.  
  136. pascal void ScrollActionProc(ControlHandle theHandle, short partCode)
  137. {
  138.     short            scrollDistance;
  139.     TEHandle        hTE;
  140.     WindowPtr        theWindow;
  141.     
  142.     theWindow=(**theHandle).contrlOwner;
  143.     if (theWindow==0L)
  144.         return;
  145.     
  146.     hTE=GetWindowTE(theWindow);
  147.     if (hTE==0L)
  148.         return;
  149.     
  150.     switch (partCode)
  151.     {
  152.         case inUpButton:
  153.         case inDownButton:
  154.             scrollDistance=(**hTE).lineHeight;
  155.             break;
  156.         case inPageUp:
  157.         case inPageDown:
  158.             scrollDistance=(**hTE).viewRect.bottom-(**hTE).viewRect.top-(**hTE).lineHeight;
  159.             break;
  160.         default:
  161.             scrollDistance=0;
  162.             break;
  163.     }
  164.     
  165.     if ((partCode==inDownButton) || (partCode==inPageDown))
  166.         scrollDistance=-scrollDistance;
  167.     
  168.     MyMoveScrollBox(theHandle, scrollDistance/(**hTE).lineHeight);
  169.     
  170.     if (scrollDistance!=0)
  171.     {
  172.         TEPinScroll(0, scrollDistance, hTE);
  173.     }
  174. }
  175.  
  176. pascal void HScrollActionProc(ControlHandle theHandle, short partCode)
  177. {
  178.     short            scrollDistance;
  179.     TEHandle        hTE;
  180.     WindowPtr        theWindow;
  181.     
  182.     theWindow=(**theHandle).contrlOwner;
  183.     if (theWindow==0L)
  184.         return;
  185.     
  186.     hTE=GetWindowTE(theWindow);
  187.     if (hTE==0L)
  188.         return;
  189.     
  190.     switch (partCode)
  191.     {
  192.         case inUpButton:
  193.         case inDownButton:
  194.             scrollDistance=20;
  195.             break;
  196.         case inPageUp:
  197.         case inPageDown:
  198.             scrollDistance=(**hTE).viewRect.right-(**hTE).viewRect.left-20;
  199.             break;
  200.         default:
  201.             scrollDistance=0;
  202.             break;
  203.     }
  204.     
  205.     if ((partCode==inDownButton) || (partCode==inPageDown))
  206.         scrollDistance=-scrollDistance;
  207.     
  208.     MyMoveScrollBox(theHandle, scrollDistance);
  209.     
  210.     if (scrollDistance!=0)
  211.     {
  212.         TEPinScroll(scrollDistance, 0, hTE);
  213.     }
  214. }
  215.  
  216. void MyMoveScrollBox(ControlHandle theControl, short scrollDistance)
  217. {
  218.     short            oldSetting, setting, max;
  219.     
  220.     oldSetting=GetControlValue(theControl);
  221.     max=GetControlMaximum(theControl);
  222.     setting=oldSetting-scrollDistance;
  223.     if (setting<0)
  224.         setting=0;
  225.     else if (setting>max)
  226.         setting=max;
  227.     
  228.     SetControlValue(theControl, setting);
  229. }
  230.  
  231. void AdjustVScrollBar(ControlHandle theControl, TEHandle hTE)
  232. {
  233.     short            oldValue, oldMax;
  234.     short            numLines, max, value;
  235.     
  236.     oldMax=GetControlMaximum(theControl);
  237.     oldValue=GetControlValue(theControl);
  238.     numLines=TotalNumberOfLines(hTE);
  239.     max=numLines-(((**hTE).viewRect.bottom-(**hTE).viewRect.top)/((**hTE).lineHeight));
  240.     if (max<0)
  241.         max=0;
  242.     SetControlMaximum(theControl, max);
  243.     value=((**hTE).viewRect.top-(**hTE).destRect.top)/((**hTE).lineHeight);
  244.     if (value<0)
  245.         value=0;
  246.     else if (value>max)
  247.         value=max;
  248.     SetControlValue(theControl, value);
  249. }
  250.  
  251. short CurrentLineNumber(TEHandle hTE)
  252. {
  253.     if (hTE==0L)
  254.         return -1;
  255.     
  256.     return LineNumberFromOffset(hTE, (**hTE).selStart);
  257. }
  258.  
  259. short LineNumberFromOffset(TEHandle hTE, short offset)
  260. {
  261.     short            i;
  262.     short            lineNumber;
  263.     short            numLines;
  264.     
  265.     if (hTE==0L)
  266.         return -1;
  267.     
  268.     lineNumber=-1;
  269.     numLines=(**hTE).nLines;
  270.     for (i=0; i<numLines; i++)
  271.     {
  272.         if ((**hTE).lineStarts[i]<=offset)
  273.             lineNumber++;
  274.         else
  275.             return lineNumber;
  276.     }
  277.     
  278.     return numLines-1;
  279. }
  280.  
  281. short LineStart(TEHandle hTE, short lineNum)
  282. {
  283.     if (hTE==0L)
  284.         return -1;
  285.     
  286.     return (**hTE).lineStarts[lineNum];
  287. }
  288.  
  289. pascal Boolean MyClikLoop(void)
  290. {
  291.     Point            thePoint;
  292.     TEHandle        hTE;
  293.     WindowPtr        theWindow;
  294.     GrafPtr            curPort;
  295.     
  296.     theWindow=GetFrontDocumentWindow();
  297.     if (theWindow==0L)
  298.         theWindow=FrontWindow();
  299.     if (theWindow==0L)
  300.         return FALSE;
  301.     
  302.     hTE=GetWindowTE(theWindow);
  303.     if (hTE==0L)
  304.         return FALSE;
  305.     
  306.     GetPort(&curPort);
  307.     SetPort(theWindow);
  308.     GetMouse(&thePoint);
  309.     if (!PtInRect(thePoint, &((**hTE).viewRect)))
  310.     {
  311.         if (thePoint.v<((**hTE).viewRect.top))
  312.         {
  313.             if ((**hTE).selStart>0)
  314.                 TEPinScroll(0, (**hTE).lineHeight, hTE);
  315.         }
  316.         else if (thePoint.v>(**hTE).viewRect.bottom)
  317.         {
  318.             if ((**hTE).selEnd<(**hTE).teLength)
  319.                 TEPinScroll(0, -(**hTE).lineHeight, hTE);
  320.         }
  321.         
  322.         AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  323.     }
  324.     
  325.     SetPort(curPort);
  326.     
  327.     return TRUE;
  328. }
  329.  
  330. void AdjustForEndScroll(ControlHandle theControl, TEHandle hTE)
  331. {
  332.     short            numLines;
  333.     short            offset;
  334.     
  335.     if (hTE==0L)
  336.         return;
  337.     
  338.     numLines=TotalNumberOfLines(hTE);
  339.     offset=numLines-GetControlMaximum(theControl)-
  340.         (((**hTE).viewRect.bottom-(**hTE).viewRect.top)/(**hTE).lineHeight);
  341.     if (offset<0)
  342.         TEPinScroll(0, -offset*((**hTE).lineHeight), hTE);
  343. }
  344.  
  345. #define kTextMargin    2
  346.  
  347. /* Return a rectangle that is inset from the portRect by the size of
  348.     the scrollbars and a little extra margin. */
  349.  
  350. void GetTERect(WindowPtr theWindow, Rect *teRect, Boolean adjustForScrollBars)
  351. {
  352.     RequireWindow();
  353.     
  354.     *teRect = theWindow->portRect;
  355.     InsetRect(teRect, kTextMargin, kTextMargin);    /* adjust for margin */
  356.     if (adjustForScrollBars)
  357.     {
  358.         teRect->bottom = teRect->bottom - 15;        /* and for the scrollbars */
  359.         teRect->right = teRect->right - 15;
  360.     }
  361. }
  362.  
  363.  
  364. /* Update the TERec's view rect so that it is the greatest multiple of
  365.     the lineHeight that still fits in the old viewRect. */
  366.  
  367. void AdjustViewRect(TEHandle docTE)
  368. {
  369.     TEPtr        te;
  370.     
  371.     if (docTE==0L)
  372.         return;
  373.     
  374.     te = *docTE;
  375.     te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / te->lineHeight)
  376.                             * te->lineHeight) + te->viewRect.top;
  377. }
  378.  
  379.  
  380. /*    Re-calculate the position and size of the viewRect and the scrollbars.
  381.     kScrollTweek compensates for off-by-one requirements of the scrollbars
  382.     to have borders coincide with the growbox. */
  383.  
  384. #define kScrollbarWidth        16
  385. #define kScrollbarAdjust    (kScrollbarWidth-1)
  386. #define kScrollTweek        2
  387.  
  388. void AdjustScrollSizes(WindowPtr window, TEHandle hTE, ControlHandle vScrollBar,
  389.     ControlHandle hScrollBar, short destOverload)
  390. {
  391.     Rect        teRect;
  392.     
  393.     if (window==0L)
  394.         return;
  395.     if (hTE==0L)
  396.         return;
  397.     
  398.     GetTERect(window, &teRect, TRUE);                            /* start with TERect */
  399.     (**hTE).viewRect = teRect;
  400.     (**hTE).destRect.left=(**hTE).viewRect.left;
  401.     (**hTE).destRect.right=(**hTE).viewRect.right+destOverload;
  402.     MoveControl(vScrollBar, window->portRect.right - kScrollbarAdjust, -1);
  403.     SizeControl(vScrollBar, kScrollbarWidth, (window->portRect.bottom - 
  404.                 window->portRect.top) - (kScrollbarAdjust - kScrollTweek));
  405.     MoveControl(hScrollBar, -1, window->portRect.bottom - kScrollbarAdjust);
  406.     SizeControl(hScrollBar, (window->portRect.right - 
  407.                 window->portRect.left) - (kScrollbarAdjust - kScrollTweek),
  408.                 kScrollbarWidth);
  409. }
  410.  
  411. void AdjustTE(TEHandle hTE, ControlHandle vScrollBar, ControlHandle hScrollBar)
  412. {
  413.     TEPtr        te;
  414.     
  415.     if (hTE==0L)
  416.         return;
  417.     if (vScrollBar==0L)
  418.         return;
  419.     if (hScrollBar==0L)
  420.         return;
  421.     
  422.     te = *hTE;
  423.     TEScroll((te->viewRect.left - te->destRect.left) -
  424.             GetControlValue(hScrollBar),
  425.             ((te->viewRect.top - te->destRect.top)/te->lineHeight) -
  426.                 GetControlValue(vScrollBar),
  427.             hTE);
  428. }
  429.  
  430. void DrawTheShadowBox(Rect theRect, Boolean eraseBackground)
  431. {
  432.     theRect.right-=2;
  433.     theRect.bottom-=2;
  434.     if (eraseBackground)
  435.         EraseRect(&theRect);
  436.     FrameRect(&theRect);
  437.     MoveTo(theRect.left+3, theRect.bottom+1);
  438.     Line(theRect.right-theRect.left-2, 0);
  439.     Line(0, -theRect.bottom+theRect.top+3);
  440.     MoveTo(theRect.left+3, theRect.bottom);
  441.     Line(theRect.right-theRect.left-3, 0);
  442.     Line(0, -theRect.bottom+theRect.top+4);
  443. }
  444.  
  445. void ZapDrawHook(TEHandle hTE)
  446. {
  447.     ProcPtr            temp;
  448.     
  449.     temp=(ProcPtr)MyDrawHook;
  450.     TECustomHook(intDrawHook, &temp, hTE);
  451.     gOldDrawHook=temp;
  452. }
  453.  
  454. void RestoreDrawHook(TEHandle hTE)
  455. {
  456.     ProcPtr            temp;
  457.     
  458.     temp=gOldDrawHook;
  459.     TECustomHook(intDrawHook, &temp, hTE);
  460. }
  461.  
  462. static    asm void MyDrawHook(void)
  463. {
  464.     rts
  465. }
  466.